package com.own.business.comment.entity.dto;

import com.own.business.comment.common.exception.CommentException;
import com.own.business.comment.entity.bo.CommentBo;
import com.own.component.common.base.entity.BaseDto;
import com.own.constant.ConstantBeanSuffix;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

/**
 * CommentDto
 *
 * @author chenxueli
 * @date 2023-07-03 14:15:52
 */
@Schema(title = "评论" + ConstantBeanSuffix.DTO)
@Getter
@Setter
@ToString
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
public class CommentDto extends BaseDto<CommentBo> implements Serializable {

    @Schema(title = "评论目标id")
    private Long targetId;

    @Schema(title = "主要内容")
    private String content;

    @Schema(title = "评论上级id")
    private Long commentId;

    /**
     * 验证数据
     */
    @Override
    public void check() {
        super.check();
        if (StringUtils.isBlank(this.content)) {
            throw new CommentException("评论的内容不能为空");
        }
        if (targetId == null) {
            throw new CommentException("评论的目标不能为空");
        }
    }

    /**
     * 转业务层对象
     *
     * @param entity 原业务层对象
     * @return 业务层对象
     */
    @Override
    public CommentBo toBo(CommentBo entity) {
        if (entity == null) {
            entity = new CommentBo();
        }
        entity.setTargetId(this.targetId);
        entity.setContent(this.content);
        entity.setCommentId(this.commentId);
        return super.toBo(entity);
    }

}

